﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 获取可以使用的表列表
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("ETL", "GetTaskList", "1.0")]
    public class GetTaskListAction : BaseAction<GetTaskListParams, GetTaskListResult>
    {
        protected override ActionResult<GetTaskListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetTaskListParams opParams)
        {
            SER.TaskService service = new SER.TaskService();
            string filter = opParams.Filter == null ? string.Empty : opParams.Filter.Trim();
            PagedList<VIW.VTaskList> result = service.GetTaskPagedList(opParams.CategoryId, filter, opParams.Page * 25, 25);
            if(result.IsSuccess)
            {
                GetTaskListResult actResult = new GetTaskListResult();
                actResult.Page = opParams.Page;
                actResult.Start = result.Start;
                actResult.Limit = result.Limit;
                actResult.Total = result.Total;

                foreach (var vItem in result.Data)
                {
                    GetTaskListResult.Item item = new GetTaskListResult.Item();
                    item.Id = vItem.Id;
                    item.Name = vItem.Name;
                    item.Creator = vItem.Creator;
                    item.CreatedTime = vItem.CreatedTime;
                    item.DtSerId = vItem.DtSerId;
                    item.DtSerText = vItem.DtSerName;
                    item.DtSourceId = vItem.DtSourceId;
                    item.DtSourceText = vItem.DtSourceName;
                    item.EnableCodeConversion = vItem.EnableCodeConversion;
                    item.EnableDataVerification = vItem.EnableDataVerification;
                    item.IsEnabled = vItem.IsEnabled;
                   item.CategoryName= vItem.CategoryName;
                    if (vItem.LastExecutedTime!= null)
                    {
                        item.LastExecutedTime = vItem.LastExecutedTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    else
                    {
                        item.LastExecutedTime = string.Empty;
                    }

                    item.RunMode = vItem.RunMode;
                    actResult.Items.Add(item);
                }
                return new ActionResult<GetTaskListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetTaskListResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class GetTaskListParams
    {
        [Parameter]
        public int Page { get; set; }

        [Parameter]
        public string Filter { get; set; }

        [Parameter]
        public Guid? CategoryId { get; set; }
    }

    public class GetTaskListResult : PagedResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            public string Name { get; set; }

            public Guid DtSerId { get; set; }

            public Guid DtSourceId { get; set; }

            public bool IsDeleted { get; set; }

            public bool IsEnabled { get; set; }

            public string Creator { get; set; }

            public DateTime CreatedTime { get; set; }

            public int RunMode { get; set; }

            public bool EnableDataVerification { get; set; }

            public bool EnableCodeConversion { get; set; }

            public string CompletedEvent { get; set; }

            public string LastExecutedTime { get; set; }

            public string DtSourceText { get; set; }

            public string DtSerText { get; set; }
            public string CategoryName { get; set; }
        }
    }
}
